﻿using System.Windows;
using System.Windows.Media;

namespace Dsl.Ped.Common.Helpers
{
    /// <summary>
    /// Helper methods for <see cref="DependencyObject"/>
    /// </summary>
    public class DependencyObjectHelper
    {
        /// <summary>
        /// Finds an ancestor of a given type
        /// </summary>
        /// <typeparam name="T">Ancestor type</typeparam>
        /// <param name="obj">Object which ancestor we are looking for</param>
        /// <returns>Ancestor of a given type, null if it does not exist</returns>
        public static T FindAncestor<T>(DependencyObject obj) where T : DependencyObject
        {
            var tmp = VisualTreeHelper.GetParent(obj);
            while (tmp != null && !(tmp is T))
            {
                tmp = VisualTreeHelper.GetParent(tmp);
            }
            return (T)tmp;
        }
    }
}
